import java.util.ArrayDeque;
import java.util.Deque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-02-27
 * Time: 11:20
 */
public class test2 {
    /**
     * LeetCode  227   基本计算器II
     */

    public int calculate(String ss) {
        Deque<Integer> s1 = new ArrayDeque<>();

        // 表示运算符
        char op = '+';
        int i = 0;
        int n = ss.length();
        char[] s = ss.toCharArray();
        while(i<n){
            if(s[i]==' '){
                i++;
            }else if(s[i]>'0' && s[i]<'9'){
                int tmp = 0;
                while(i<n && s[i]>'0' && s[i]<'9'){
                    tmp = tmp * 10 + (s[i]-'0');
                    i++;
                }
                if(op=='+'){
                    s1.push(tmp);
                }else if(op=='-'){
                    s1.push(-tmp);
                } else if (op == '*') {
                    s1.push(s1.pop()*tmp);
                }else {
                    s1.push(s1.pop()/tmp);
                }
            }else {
                op = s[i];
                i++;
            }
        }
        int ret = 0;
        while(!s1.isEmpty()){
            ret += s1.pop();
        }
        return ret;
    }
}
